﻿using BLL;
using Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 后整理管理系统.其他窗体;
using 纺织贸易管理系统;
using 纺织贸易管理系统.设置窗体;

namespace 后整理管理系统.报表窗体
{
    public partial class 排产单列表 : Form
    {
        public 排产单列表()
        {
            InitializeComponent();
            CreateGrid.Create("流程卡查询", gridView1,"uiDataGridView1");
            uiDatePicker2.Value = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
            uiDatePicker1.Value = uiDatePicker2.Value.AddDays(-QueryTime.间隔);
            Query();
        }
        private void Query()
        {
            gridControl2.DataSource = Connect.dbcontext.Queryable<ScheduleDoc>().Where(x => x.ScheduleDate >= uiDatePicker1.Value.Date && x.ScheduleDate <= uiDatePicker2.Value.Date.AddDays(1)).ToList();
        }

        private void uiDataGridView1_Click(object sender, EventArgs e)
        {
            var doc=uiDataGridView1.GetFocusedRow() as ScheduleDoc;
            if (doc != null)
            {
                gridControl1.DataSource = LiuzhuancardService.GetLiuzhuancardlst(x => x.ArrangementDoc == doc.DocNum);
                要求赋值(doc.DocNum);
            }
        }
        private void 要求赋值(string docnum)
        {
            var requirements = Connect.dbcontext.Queryable<ProofingRequirements>().First(x => x.DocNum == docnum)??new ProofingRequirements();
            txtzhengfangmian.Text = requirements.FrontOrBack;
            txtshuiya.Text = requirements.Hydraulic;
            txtceshibiaozhun.Text = requirements.TestStandards;
            txtqiebian.Text = requirements.Trimming;
            txtshuixi.Text = requirements.Washing;
            txtfangshuixiaoguo.Text = requirements.WaterproofEffect;
            txtkezhong.Text = requirements.Weight;
            txtmenfu.Text = requirements.Width;
            txtgunxing.Text = requirements.Sticktype;
            txtjiaoshui.Text = requirements.Glue;
            txtdibu.Text = requirements.TPU;
            txtwendu.Text = requirements.Temperature;
            txttieheyali.Text = requirements.FitPressure;
            txtshoujuanyali.Text = requirements.RewindingPressure;
            txtshoujuanzhangli.Text = requirements.RewindingTension;
            TxtTopWeight.Text = requirements.TopWeight;
            txtBottomWeight.Text = requirements.BottomWeight;
            txtSeamSlippage.Text = requirements.SeamSlippage;
            txtDetectionmetrics.Text = requirements.DetectionMetrics;
            txtTearStrength.Text = requirements.TestStandards;
            txtOther.Text = requirements.Other;
            txtdivest.Text = requirements.Divest;
            txtgunxing1.Text = requirements.Sticktype1;
            txtjiaoshui1.Text = requirements.Glue1;
            txtguhuaji.Text = requirements.CuringAgent;
            txtrongji.Text = requirements.Solvent;
            txtguhuaji1.Text = requirements.CuringAgent1;
            txtrongji1.Text = requirements.Solvent1;
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Query();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (AccessBLL.CheckAccess("删除排产单"))
            {
                var doc = uiDataGridView1.GetFocusedRow() as ScheduleDoc;
                if (doc != null)
                {
                    if (this.ShowAskDialog("你确定要删除该排产单号吗？单号为" + doc.DocNum))
                    {
                        try
                        {
                            Connect.dbcontext.BeginTran();
                            Connect.dbcontext.Deleteable(doc).ExecuteCommand();
                            Connect.dbcontext.Deleteable<ProofingRequirements>().Where(x => x.DocNum == doc.DocNum).ExecuteCommand();
                            Connect.dbcontext.Updateable<Liuzhuancard>().SetColumns(x => new Liuzhuancard() { ArrangementDate = string.Empty, ArrangementDoc = string.Empty, ArrangementMan = string.Empty, IsArrangementProduced = false }).Where(x => x.ArrangementDoc == doc.DocNum).ExecuteCommand();
                            Connect.dbcontext.CommitTran();
                            this.ShowSuccessDialog("删除成功");
                            Query();
                        }
                        catch (Exception ex)
                        {
                            Connect.dbcontext.RollbackTran();
                            this.ShowErrorDialog("删除排产单的时候发生了错误!" + ex.Message);
                        }
                    }
                }
            }
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var doc = uiDataGridView1.GetFocusedRow() as ScheduleDoc;
            if (doc != null)
            {
                var fm = new 工艺二次登记(gridControl1.DataSource as List<Liuzhuancard>, FormUseful.删除, doc);
                fm.ShowDialog();
                Query();
            }
        }
    }
}
